<?php

namespace App\Http\Controllers\Merchants;
session_start();
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;

class ShangController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view("home.zhuce");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        
        //检测验证码是否一致
        if(session('code')!=$request->code){
            return back()->with('error','验证码错误!');
        }
        //检测用户名是否存在
        $user = DB::table('merchant')->where('email',$request['email'])->first();
    if(!($user)){
        return back();
    }
        $userpass = Hash::make($user->password);

        //检测密码是否一致
        if(Hash::check($user->password,$userpass)){
            session(['homeInfo'=>$user]);
            //$_SESSION['homeInfo'] = $user;

            if($user->status ==1){
                //备注带了id
                return redirect('/')->with('id',$user['id']);
            }elseif($user->status ==2){

                $id=session('homeInfo')->id;
               
            return redirect("/news/{$id}");
            }
        }else{
            return back();
            
        }   
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //检测用户名是否已被使用
        $user = DB::table('merchant')->where('email',$request->email)->first();
       
        if($user){
            return back()->with('error','邮箱已存在');
        }
        //处理数据
        $data = $request->except(['_token','repassword']);
        $data['password']=Hash::make($data['password']);
        
        $data['status']=2;
     
        
        $user = DB::table('merchant')->insert($data);
       
        if($user){
            
            return redirect('/')->with('success','注册成功');
        }else{
            return back()->with('error','注册失败');
        }
        
    }

    //验证码
    public function code()
    {
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
     
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
       
        //将session写入闪存
        session(['code'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

        //执行用户退出
    public function logout()
    {
        session()->forget('homeInfo');
        unset($_SESSION['homeInfo']);
        return redirect('/');
    }

    //显示订单
    public function indent($status,$id){
        if($status == 1){
        $data = DB::table('indent')->where('mid',$id)->where('status',$status)->paginate(5);
          return view('merchant.dingdan')->with('data',$data);
      }
      if($status == 2){
        $data = DB::table('indent')->where('mid',$id)->where('status',$status)->paginate(5);
          return view('merchant.dingdan2')->with('data',$data);
      }

      if($status == 3){
        $data = DB::table('indent')->where('mid',$id)->where('status',$status)->paginate(5);
          return view('merchant.dingdan3')->with('data',$data);
      }

        if($status == 4){
        $data = DB::table('indent')->where('mid',$id)->where('status',$status)->paginate(5);
          return view('merchant.dingdan4')->with('data',$data);
      }

    }

    public function status($id){
        DB::table('indent')
            ->where('id', $id)
            ->update(['status' => 2]);
            return back();
    }


}
